package com.heima.article.listener;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.pojos.WmNews;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class WmNewsListener {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired ApArticleService apArticleService;
    @KafkaListener(topics = "wmnews-topic")
    public void onMessage(String message) {
        if (!StringUtils.isEmpty(message)) {
            WmNews wmNews = JSON.parseObject(message, WmNews.class);
            ApArticleConfig apArticleConfig = apArticleConfigMapper.selectById(wmNews.getArticleId());
            apArticleConfig.setIsDown(wmNews.getEnable()==1?true:false);
            ApArticle apArticle = apArticleService.getById(wmNews.getArticleId());
            apArticle.setSyncStatus(wmNews.getEnable()==1?true:false);
            apArticleService.updateById(apArticle);
        }
    }
}